package quizman;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dsrg.servlet.app.frontController.Servlet;

import quizman.domain.Quiz;
public class EditQuizPageController extends Servlet {

	private static final long serialVersionUID = 1L;

	protected void processRequest(
			HttpServletRequest request,
			HttpServletResponse response)
	throws ServletException, java.io.IOException //
	{
		String qname = request.getParameter("quizName");
		try {
			String quizLength = request.getParameter("quizLength");
			String quizBegin = request.getParameter("quizBegin");
			String quizEnd = request.getParameter("quizEnd");
			Quiz q;
			if(quizLength==null||quizEnd==null||quizEnd==null)
			{
				q = ViewQuizTS.execute(qname);
			}
			else
			{
				q = EditQuizTS.execute(qname, quizLength, quizBegin, quizEnd);
			}
			if(q==null)
				throw new RuntimeException("Quiz not in database");
			request.setAttribute("quiz", q);
			getDispatcher("EditQuiz.jsp", request).forward(request, response);
		} catch (Exception e) {
			request.setAttribute("error", e);
			request.setAttribute("message", "Error processing in servlet "+getClass().getSimpleName()+",\n"+
					"while editing quiz: "+qname+".\n");
			getDispatcher("Error.jsp", request).forward(request, response);
		}
	}
}
